﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;

namespace Cartoon.Model
{
    public class Chapter
    {
        public int Id { get; set; }
        public string Src { get; set; }
        public List<string> PicListUrl { get; set; }
        public int PicCount { get; set; }   
        public Comic comic { get; set; } 
        public long PicAllSize { get; set; }
        public string PicListUrlString { get; set; }
        //public string ChapterDomain;
        public DataTable ToDataTable()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("ComicId");
            dt.Columns.Add("Id");
            dt.Columns.Add("Src");
            dt.Columns.Add("PicListUrl", new object().GetType());
            dt.Columns.Add("PicCount");
            dt.Columns.Add("PicAllSize");
            dt.Columns.Add("PicListUrlString");
            //dt.Columns.Add("ChapterDomain");

            DataRow newRow = dt.NewRow();
            newRow["ComicId"] = this.comic.Id;
            newRow["Id"] = this.Id;
            newRow["Src"] = this.Src;
            newRow["PicListUrl"] = this.PicListUrlString;
            newRow["PicCount"] = this.PicCount;
            newRow["PicAllSize"] = this.PicAllSize;
            newRow["PicListUrlString"] = this.PicListUrlString;
            //newRow["ChapterDomain"] = this.ChapterDomain;

            dt.Rows.Add(newRow);

            dt.TableName = "《 " + this.comic.Name + " 》第" + this.Id + "话";
            return dt;
        }

        public void Save(string filename)
        {
            FileInfo fileInfo = new FileInfo(filename);
            if (fileInfo.Exists == false)
            {
                Directory.CreateDirectory(fileInfo.DirectoryName);
                using (FileStream fs = File.Create(fileInfo.FullName))
                {
                    fs.Close();
                }
            }

            DataSet ds = new DataSet();
            ds.Tables.Add(this.ToDataTable());
            ds.WriteXml(filename);
        }

        public Chapter Load(string filepath)
        {

            Chapter chp = new Chapter();
            chp.comic = this.comic;
            chp.Id = this.comic.Id;
            chp.PicAllSize = this.PicAllSize;
            chp.PicCount = this.PicCount;
            chp.PicListUrl = this.PicListUrl;
            chp.PicListUrlString = this.PicListUrlString;
            //chp.ChapterDomain = this.ChapterDomain;

            DataSet ds = new DataSet();
            DataTable dt = new DataTable();

            dt.TableName = "《 " + this.comic.Name + " 》第" + this.Id + "话";

            try
            {
                ds.ReadXml(filepath);
                dt = ds.Tables[0];
            }
            catch (Exception ignore) { return chp; }

            if (dt == null || dt.Rows.Count == 0) return chp;

            foreach (DataRow row in dt.Rows)
            { 
                chp.PicCount = Convert.ToInt32(row["PicCount"]);
                chp.PicAllSize = Convert.ToInt64(row["PicAllSize"]);
                chp.PicListUrl = row["PicListUrl"] == null ? new List<string>() : row["PicListUrl"].ToString().Split('|').ToList();
                chp.PicListUrlString = row["PicListUrlString"].ToString();
                //chp.ChapterDomain = row["ChapterDomain"].ToString();
            }

            return chp;
        }

    }
}
